package top.chmin.project.cloud.sys.dict.info;

import java.util.List;
import java.util.ArrayList;
import java.time.LocalDateTime;

import top.chmin.project.cloud.tool.LocalTool;
import top.chmin.project.cloud.common.*;
import lombok.experimental.Accessors;
import lombok.Setter;
import lombok.Getter;



/**
 * chmin
 * 2024-01-26 14:02:35
 * cloud-code
 */
@Getter
@Setter
@Accessors(chain = true)
public class DictInfo extends Model {
    /*###########auto#start###########*/
    // column编号
    private String columnCode;
    // 字典编码
    private String dictCode;
    /*###########auto##end############*/
    /* append $modelFieldAppender$ */

    public DictInfoModelCondition forInsert() {
        return new DictInfoModelCondition().forInsert();
    }

    public DictInfoModelCondition forUpdate() {
        return new DictInfoModelCondition().forUpdate();
    }

    public DictInfoModelCondition forDelete() {
        return new DictInfoModelCondition().forDelete();
    }

    public DictInfoModelCondition forSelect() {
        return new DictInfoModelCondition().forSelect();
    }

    public class DictInfoModelCondition extends Condition {
        DictInfoModelCondition parent = null;

        private DictInfoModelCondition() {
        }

        private DictInfoModelCondition(DictInfoModelCondition condition) {
            this.parent = condition;
        }

        private DictInfoModelCondition forInsert() {
            DictInfoModelCondition condition = new DictInfoModelCondition();
            List<String> columnNameList = new ArrayList<>();
            List<String> valueList = new ArrayList<>();
            /*###########auto#start###########*/
            columnNameList.add("column_code");
            valueList.add("?");
            condition.getValueList().add(getColumnCode());

            columnNameList.add("dict_code");
            valueList.add("?");
            condition.getValueList().add(getDictCode());

            /*###########auto##end############*/
            /* append $modelForInsertFieldAppender$ */
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalDateTime.now());
            condition.getValueList().add(LocalDateTime.now());
            condition.setSql("INSERT INTO sys_dict_info(%s, create_by, update_by, create_time, update_time) VALUES (%s, ?, ?, ?, ?)".formatted(
                    String.join(", ", columnNameList),
                    String.join(", ", valueList)
            ));
            return condition;
        }

        private DictInfoModelCondition forUpdate() {
            DictInfoModelCondition condition = new DictInfoModelCondition();
            /*###########auto#start###########*/
            if (columnCode != null) {
                condition.getUpdateList().add("column_code = ?");
                condition.getUpdateValueList().add(columnCode);
            }

            if (dictCode != null) {
                condition.getUpdateList().add("dict_code = ?");
                condition.getUpdateValueList().add(dictCode);
            }

            /*###########auto##end############*/
            /* append $modelForUpdateFieldAppender$ */
            if (getUpdateBy() != null) {
                condition.getUpdateList().add("update_by = ?");
                condition.getValueList().add(LocalTool.getUserId());
            }
            if (getUpdateTime() != null) {
                condition.getUpdateList().add("update_time = ?");
                condition.getValueList().add(LocalDateTime.now());
            }
            condition.setSql("UPDATE sys_dict_info#update##where##order##limit#");
            return condition;
        }

        private DictInfoModelCondition forDelete() {
            DictInfoModelCondition condition = new DictInfoModelCondition();
            condition.setSql("DELETE FROM sys_dict_info#where##group##order##limit#");
            return condition;
        }

        private DictInfoModelCondition forSelect() {
            DictInfoModelCondition condition = new DictInfoModelCondition();
            condition.modelClass = DictInfo.class;
            condition.setSql("SELECT #select# FROM sys_dict_info#where##group##order##limit#");
            return condition;
        }

        public DictInfoModelCondition distinct() {
            this.distinct = true;
            return this;
        }

        public DictInfoModelCondition limit(int limit) {
            this.limit = " LIMIT " + limit;
            return this;
        }

        public DictInfoModelCondition sub() {
            return new DictInfoModelCondition(this);
        }

        public DictInfoModelCondition finish() {
            if (!whereList.isEmpty()) {
                parent.whereList.add("(" + String.join(" OR ", whereList) + ")");
            }
            parent.whereValueList.addAll(whereValueList);
            return parent;
        }

        public DictInfoCondition toFlex() {
            DictInfoCondition flex = new DictInfoCondition();
            flex.setSql(sql);
            flex.setDistinct(distinct);
            flex.setModelClass(modelClass);
            flex.setSelectList(selectList);
            flex.setUpdateList(updateList);
            flex.setUpdateValueList(updateValueList);
            flex.setWhereList(whereList);
            flex.setWhereValueList(whereValueList);
            flex.setGroupList(groupList);
            flex.setOrderList(orderList);
            flex.setLimit(limit);
            return flex;
        }

        /*###########auto#start###########*/
        public ColumnCode columnCode() {
            return new ColumnCode();
        }

        public class ColumnCode extends StringModelColumn<DictInfoModelCondition> {
            private ColumnCode() {
                super(DictInfoModelCondition.this, "column_code", DictInfo.this.getColumnCode());
            }
        }

        public CreateBy createBy() {
            return new CreateBy();
        }

        public class CreateBy extends StringModelColumn<DictInfoModelCondition> {
            private CreateBy() {
                super(DictInfoModelCondition.this, "create_by", DictInfo.this.getCreateBy());
            }
        }

        public CreateTime createTime() {
            return new CreateTime();
        }

        public class CreateTime extends LocalDateTimeModelColumn<DictInfoModelCondition> {
            private CreateTime() {
                super(DictInfoModelCondition.this, "create_time", DictInfo.this.getCreateTime());
            }
        }

        public DictCode dictCode() {
            return new DictCode();
        }

        public class DictCode extends StringModelColumn<DictInfoModelCondition> {
            private DictCode() {
                super(DictInfoModelCondition.this, "dict_code", DictInfo.this.getDictCode());
            }
        }

        public Id id() {
            return new Id();
        }

        public class Id extends IntegerModelColumn<DictInfoModelCondition> {
            private Id() {
                super(DictInfoModelCondition.this, "id", DictInfo.this.getId());
            }
        }

        public UpdateBy updateBy() {
            return new UpdateBy();
        }

        public class UpdateBy extends StringModelColumn<DictInfoModelCondition> {
            private UpdateBy() {
                super(DictInfoModelCondition.this, "update_by", DictInfo.this.getUpdateBy());
            }
        }

        public UpdateTime updateTime() {
            return new UpdateTime();
        }

        public class UpdateTime extends LocalDateTimeModelColumn<DictInfoModelCondition> {
            private UpdateTime() {
                super(DictInfoModelCondition.this, "update_time", DictInfo.this.getUpdateTime());
            }
        }

        /*###########auto##end############*/
        /* append $modelConditionFieldAppender$ */
    }
}
